# -*- coding: utf-8 -*-
# Description: Celery任务示例
# Created: dingwenjing 2022/11/29
import traceback

from celery.utils.log import get_task_logger
from app.task.celery_app import celery
from datetime import datetime

from app.main.service.cron_service import cron_demo

# 获取celery执行器的日志记录器
logger = get_task_logger('celery_worker')


# 定义定时任务和异步任务，定时任务还需要在config中配置执行周期
@celery.task(ignore_result=True)
def cron_task_demo():
    """定时任务 demo"""
    try:
        logger.info('==========cron_demo_start=============')
        start_time = datetime.now()
        cron_demo()
    except Exception:
        logger.error(traceback.format_exc())
    finally:
        end_time = datetime.now()
        logger.info('==========cron_demo execution time:{}==============='.format((end_time - start_time).seconds))
        logger.info('==========cron_demo_end===============')


@celery.task(ignore_result=True)
def async_task():
    """异步 do something"""


if __name__ == '__main__':
    pass
